package com.kotlin.flong.flow

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import java.lang.Exception

/**
 * @Author:liangjl
 * @Date: 2020/7/11-20:41
 * @Eamil:jilongliang@sina.com
 * @Description:
 */
private fun myMethod(): Flow<Int> = flow {

    for (a in 1..3) {
        println("emiting $a")
        emit(a)
    }
}

fun main() = runBlocking<Unit> {

    try {
        myMethod().collect { value ->
            println(value)
            check(value <= 1) {
                "Collect $value"
            }
        }
    } catch (e: Exception) {
        println(e.message)
    }
}